package cn.xiaohuodui.service;

import cn.xiaohuodui.constant.ErrorCodeDefine;
import cn.xiaohuodui.dao.BrandMapper;
import cn.xiaohuodui.exceptions.BussException;
import cn.xiaohuodui.form.PageForm;
import cn.xiaohuodui.form.back.brand.BackBrandCreateForm;
import cn.xiaohuodui.form.back.brand.BackBrandDo;
import cn.xiaohuodui.form.back.brand.BackBrandQueryForm;
import cn.xiaohuodui.model.Brand;
import cn.xiaohuodui.res.JsonRes;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Created by TR on 2018/4/2.
 */
@Service
@Slf4j
public class BrandService {

    @Autowired
    BrandMapper brandMapper;


    public JsonRes<Map<String,Object>>  list(PageForm form){

        List<Brand> brandList = brandMapper.query(form.toMap());
        int total = brandMapper.queryTotalCount();
        int totalPages = (int) Math.ceil((double) total / form.getSize());
        Map<String, Object> data = new HashMap();
        data.put("brandList", brandList);
        data.put("totalPages", totalPages);
        return JsonRes.createBySuccessData(data);
    }

    public JsonRes<Map<String,Object>> detail(Integer id) throws BussException {

        Brand entity =brandMapper.selectByPrimaryKey(id);

        if(entity == null){
            throw new BussException(ErrorCodeDefine.BRAND_NOT_EXIST.getCode(),ErrorCodeDefine.BRAND_NOT_EXIST.getDesc());
        }
        Map<String, Object> data = new HashMap();
        data.put("brand",entity);
        return JsonRes.createBySuccessData(data);
    }












    // ------------------------------------------------> 以下为后台代码

    /**
     * 新建品牌
     *
     * @param backGoodCreateForm
     * @return
     */
    public Brand createBrand(BackBrandCreateForm backGoodCreateForm) {
        Brand brand = new Brand();
        BeanUtils.copyProperties(backGoodCreateForm, brand);
        brand.setCreated(System.currentTimeMillis());
        brand.setUpdated(brand.getCreated());
        brand.setDeleted(Byte.valueOf("0"));

        brandMapper.insert(brand);

        return brand;
    }

    /**
     * 查询 brand数量
     *
     * @param form
     * @return
     */
    public int brandBackCount(BackBrandQueryForm form) {
        Map qm = new HashMap();
        qm.put("name", form.getName());
        qm.put("enName", form.getEnName());
        return brandMapper.brandBackCount(qm);
    }

    /**
     * 查询 brand
     *
     * @param form
     * @return
     */
    public List<BackBrandDo> brandBackQuery(BackBrandQueryForm form) {
        Map qm = new HashMap();
        qm.put("limit", form.getLimit());
        qm.put("offset", form.getOffset());

        qm.put("name", form.getName());
        qm.put("enName", form.getEnName());

        List<Brand> goodList = brandMapper.brandBackQuery(qm);
        if (CollectionUtils.isEmpty(goodList)) {
            return new ArrayList<>();
        }
        return goodList.stream().map(eachListDo -> {
            BackBrandDo brandDo = new BackBrandDo();
            BeanUtils.copyProperties(eachListDo, brandDo);
//            ActivityPublish activity = activityPublishMapper.selectByPrimaryKey(eachListDo.getActivityId());
//            if (activity != null){
//                goodDo.setTitle(activity.getTitle());
//                ActivityGoods good = activityGoodsMapper.selectByPrimaryKey(activity.getActivityGoodId());
//                if (good != null) {
//                    CropType cropType = cropTypeMapper.selectByPrimaryKey(good.getCropTypeId());
//                    goodDo.setCropTypeName(cropType.getTypeName());
//                    goodDo.setCropName(good.getGoodsName());
////                    activityOrderDo.setWeight(good.getWeight());
////                    activityVo.setAmount(good.getAmount());
////                    activityVo.setUsed(good.getUsed());
//                    goodDo.setYear(good.getYear());
////                    activityVo.setOrigin(good.getOrigin());
////                    activityVo.setCropIntro(cropType.getIntro());
//                }
//            }

            return brandDo;

        }).collect(Collectors.toList());
    }


}
